// pages/tabBar/information/index.js
import {http} from '../../../utils/apiutils'
import {input} from '../../../utils/tools'
import {timeago} from '../../../utils/timeago'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mt:0,
        mh:0,
        t1:1,
        loading:false,
        hasMore:true,
        pageNo:1,
        pageSize:20,
        dataList:[],
        keyword:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let statusBar = wx.getMenuButtonBoundingClientRect();
        let sysInfo = wx.getSystemInfoSync();
        this.setData({
            mt:statusBar.top,
            mh:statusBar.height,
            mmh:(statusBar.top-sysInfo.statusBarHeight)*2+statusBar.height,
            smh:statusBar.top-sysInfo.statusBarHeight
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.data.loading=false;
        this.data.hasMore=true;
        this.data.pageNo=1;
        this.data.dataList=[]
        this.getList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareObj = {
          title: "资讯", // 默认是小程序的名称(可以写slogan等)
          path:'/pages/index/index?route=information&code='+getApp().globalData.userInfo.inviteCode||'',
          success: function (res) {
            // 转发成功之后的回调
            if (res.errMsg == 'shareAppMessage:ok') {
            }
          }
        }
        return shareObj;
      },
    t1change(e){
        let key= e.currentTarget.dataset.key;
        this.setData({
            t1:key,
            pageNo:1,
            dataList:[],
            hasMore:true
        })
        wx.nextTick(_=>{
            this.getList()
        })
    },
    getList(){
        if(this.data.loading||!this.data.hasMore) return;
        this.data.loading = true;
        wx.showLoading()
        let params={
            pageNo:this.data.pageNo,
            pageSize:this.data.pageSize,
            key:this.data.keyword,  
        }
        if(this.data.t1==1){
            http.newslist(params,res=>{
                if(res.code==0){
                    if(res.data.records.length>0){
                        var reg = new RegExp("(<img.*src=\"\.*?\>)"); 
                        res.data.records.map(item=>{
                            let time  = new Date(Date.parse(item.createTime.replace(/-/g,  "/")));
                            item.time = timeago(new Date(time).getTime())
                            if(item.content){
                                let imgarr = item.content.match(/<img\s*src=\"([^\"]*?)\"[^>]*>/gi);
                                if(imgarr){
                                    let re = /src="([^"]*)"/g
                                    item.img = re.exec(imgarr[0])[1]
                                    item.hasImg = true;
                                }else{
                                    item.hasImg = false;
                                }
                            }
                        })
                        this.setData({
                            dataList:[...this.data.dataList,...res.data.records],
                            pageNo:this.data.pageNo+1
                        })
                        console.log(this.data.dataList)
                    }else{
                        this.data.hasMore = false
                    }
                }
                wx.nextTick(()=>{
                    this.data.loading = false;
                    wx.hideLoading()
                })
            },err=>{
                this.data.loading = false
            })
        }else{
            http.newsmylist(params,res=>{
                if(res.code==0){
                    if(res.data.records.length>0){
                        var reg = new RegExp("(<img.*src=\"\.*?\>)"); 
                        res.data.records.map(item=>{
                            let time  = new Date(Date.parse(item.createTime.replace(/-/g,  "/")));
                            item.time = timeago(new Date(time).getTime())
                            if(item.content){
                                let imgarr = item.content.match(/<img\s*src=\"([^\"]*?)\"[^>]*>/gi);
                                if(imgarr){
                                    let re = /src="([^"]*)"/g
                                    item.img = re.exec(imgarr[0])[1]
                                    item.hasImg = true;
                                }else{
                                    item.hasImg = false;
                                }
                            }
                        })
                        this.setData({
                            dataList:[...this.data.dataList,...res.data.records],
                            pageNo:this.data.pageNo+1
                        })
                    }else{
                        this.data.hasMore = false
                    }
                }
                wx.nextTick(()=>{
                    this.data.loading = false;
                    wx.hideLoading()
                })
            },err=>{
                this.data.loading = false
            })
        }
        
    },
    input:function(e){
        input.bind(this)(e)
    },
    searchkey(){
        this.data.dataList=[];
        this.data.pageNo=1;
        this.getList()
    },
    tonew(){
        wx.navigateTo({
          url: '../../information/pages/new-pub/index',
        })
    },
    todtl(e){
        let item = JSON.stringify(e.currentTarget.dataset.item);
        wx.navigateTo({
            url: '/pages/information/pages/list-dtl/index?item='+encodeURIComponent(item),
        })
    }
})